package 力扣热题100;

public class LeetCode18 {
    public int[] searchRange(int[] nums, int target) {
        if(nums.length==0){
            return new int[]{-1,-1};
        }
        int l=0;
        int r=nums.length-1;
        int[]arr=new int[2];
        while (l<=r){
            int mid=l+(r-l)/2;
            if(nums[mid]==target){
                int goleft=mid;
                int goright=mid;
                while (goleft>0&&nums[goleft]==target){//在确保没有越界的前提下进行相等判断
                    goleft--;
                }
                arr[0]=nums[goleft]==target?0:goleft+1;//判断边界情况
                while (goright<nums.length-1&&nums[goright]==target){
                    goright++;
                }
                arr[1]=nums[goright]==target? nums.length-1:goright-1;
                return arr;
            }
            if(nums[mid]>target){
                r=mid-1;
            }else if(nums[mid]<target) {
                l=mid+1;
            }
        }
        return new int[]{-1,-1};
    }

    public static void main(String[] args) {
        int arr[]={1};
        LeetCode18 leetCode18=new LeetCode18();
        leetCode18.searchRange(arr,1);
    }
}
